<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Python JSON-RPC demo</title>

</head>
<body>
 <h1>Python JSON-RPC demo</h1>

  This small demo uses the Python SimpleJSONRPCServer module, with
  the Dojo JSON-RPC client.<br /><br />

  <div id="loading">(Please wait while dojo json-rpc client loads...)</div>

<!--
<script type="text/javascript">
djConfig = {isDebug:true};
</script>
<script type="text/javascript" src="../js/dojo/dojo.js"></script>
-->

<script type="text/javascript" src="dojo_rpc_only.js"></script>

<script type="text/javascript">

//dojo.require("dojo.debug.console");

// load up the required dojo json-rpc stuff
//dojo.require("dojo.event.*");
//dojo.require("dojo.io.*");

//dojo.require("dojo.rpc.*");

// spec up the remote json-rpc service
hostDef = {
      "serviceType": "JSON-RPC", 
      "serviceURL": "jsonrpctest.cgi",
      "sync": true,
      "methods":[ 
        {
          "name": "add", 
          "parameters":[
            {"name": "x"},
            {"name": "y"}  
          ]
        },
        {
          "name": "subtract", 
          "parameters":[
            {"name": "x"},
            {"name": "y"}  
          ]
        }
       ]
    }

// create json-rpc service
host = new dojo.rpc.JsonService({smdObj:hostDef});

// callback triggered by the 'Calculate' button on the page
function doCalculate() {

    var x, y, ret;

    // fetch x and y from fields on page
    x = parseFloat(document.getElementById("x").value);
    y = parseFloat(document.getElementById("y").value);

    // invoke the method synchronously, get result
    // (you could do it asynchronously, by passing a third argument,
    // a function which takes a single argument - result object)

    //var myDeferred = host.add(x,y).addCallback(onCalculateDone);
    var myDeferred = host.callRemote("pow", [x,y])
    myDeferred.addCallback(onCalculateDone);
    myDeferred.addErrback(onCalculateError);
}

// callback upon completion of json-rpc request
function onCalculateDone(result) {
    // display result on page
    document.getElementById("result").textContent = dojo.json.serialize(result);
}

// callback upon error
function onCalculateError(err) {
    alert("ERROR: "+err.message);
}

dojo.byId("loading").innerHTML = "";

</script>


  <table>
   <tr>
    <td>pow(</td>
    <td><input type="text" id="x" name="x" size="3"></td>
    <td>, </td>
    <td><input type="text" id="y" name="y" size="3"></td>
    <td>) = </td>
    <td><span id="result"></span></td>
   </tr>
    <td colspan="6"><button onclick="doCalculate();">Calculate!</button></td>
   <tr>
   </tr>
  </table>

</body>
</html>
